package com.sxkiler.demo.test.elasticsearch;

import cn.hutool.core.bean.BeanUtil;
import com.sxkiler.demo.test.apollo.ApolloUtil;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 新增工具类获取es的配置
 */
public class BaseESStartFactory {

    private static ConcurrentHashMap<String,BaseESStarter> esMap = new ConcurrentHashMap<String,BaseESStarter>();

    public static BaseESStarter getStarter(String name, String namespace, String apolloPrefix) throws Exception{
        BaseESStarter starter = esMap.get(name);
        if(starter==null){
            Map<String,Object> configs = ApolloUtil.getApolloConfig(namespace,apolloPrefix);
            starter = getStarter(name,configs);
        }
        return starter;
    }

    public static BaseESStarter getStarter(String name, Map configs) throws Exception{
        BaseESStarter starter = esMap.get(name);
        if(starter == null && configs != null){
            BaseESProperties properties = BeanUtil.mapToBean(configs, BaseESProperties.class,false);
            starter = new BaseESStarter(properties);
            starter.start();
            esMap.put(name,starter);
        }
        return starter;
    }
    public static BaseESStarter getStarter(String name, BaseESProperties properties) throws Exception{
        BaseESStarter starter = esMap.get(name);
        if(starter == null && properties != null){
            starter = new BaseESStarter(properties);
            starter.start();
            esMap.put(name,starter);
        }
        return starter;
    }
}
